package com.json.controller;

import com.alibaba.fastjson2.JSON;
import com.json.entity.ResponseResult;
import com.json.entity.User;
import com.json.service.UserService;
import com.json.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws SecurityException, IOException {
        //设置请求和响应代码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //设置响应类型
        resp.setContentType("application/json;charset = utf-8");
        //获得请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //调用用户业务逻辑对象进行登录验证
        UserService userService = new UserServiceImpl();
        User user = userService.login(username,password);
        //转化为json, 发送给前端
        String str = JSON.toJSONString(new ResponseResult(200,"ok",user));
        resp.getWriter().write(str);
    }
}
